# $Id: PKGBUILD 84430 2013-02-18 23:24:23Z svenstaro $
# Maintainer: Sven-Hendrik Haase <sh@lutzhaase.com>
# Contributor: Chris Brannon <cmbrannon79@gmail.com>
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Contributor: Anders Bergh <anders1@gmail.com>
# Contributor: Alexander Fehr <pizzapunk gmail com>

pkgname=('dmd' 'libphobos')
pkgbase=dmd
pkgver=2.062
pkgrel=1
arch=('i686' 'x86_64')
url="http://www.digitalmars.com/d/2.0/"
#source=(http://ftp.digitalmars.com/$pkgname.$pkgver.zip
source=(http://downloads.dlang.org.s3-website-us-east-1.amazonaws.com/releases/2013/dmd.${pkgver}.zip
        tools-${pkgver}.tar.gz::https://github.com/D-Programming-Language/tools/tarball/v${pkgver})
license=('custom')
md5sums=('fd2211206532ab41a8aef764a9225d3c'
         '2e301d06071fb24c7c2d504c8ddbbc39')

[[ $CARCH == "x86_64" ]] && _archbits="64"
[[ $CARCH == "i686" ]] && _archbits="32"

build() {
    cd $srcdir/dmd2/src/

    echo "$pkgver" > VERSION # Fix specific release bug of 2.062
    cd dmd
    make -f posix.mak MODEL=$_archbits

    cd ../druntime
    make -f posix.mak MODEL=$_archbits DMD=../dmd/dmd

    cd ../phobos
    make -f posix.mak MODEL=$_archbits DMD=../dmd/dmd

    # Building rdmd ourselves breaks in dmd 2.059
    #cd $srcdir/D-Programming-Language-tools-*
    #$srcdir/dmd2/src/dmd/dmd -I$srcdir/dmd2/src/druntime/import/ -I$srcdir/dmd2/src/phobos -L-L$srcdir/dmd2/src/phobos/generated/linux/release/$_archbits/ rdmd.d
}

package_dmd() {
  pkgdesc="The Digital Mars D compiler"
  backup=('etc/dmd.conf')
  depends=('libphobos' 'gcc')

  install -Dm755 $srcdir/dmd2/src/dmd/dmd $pkgdir/usr/bin/dmd

  mkdir -p $pkgdir/etc
  echo -e "[Environment]\nDFLAGS=-I/usr/include/d -I/usr/include/d/druntime/import -L-L/usr/lib -L-L/usr/lib32 -L--no-warn-search-mismatch -L--export-dynamic" > $pkgdir/etc/dmd.conf


  install -Dm644 $srcdir/dmd2/man/man1/dmd.1 $pkgdir/usr/share/man/man1/dmd.1
  install -Dm644 $srcdir/dmd2/man/man1/rdmd.1 $pkgdir/usr/share/man/man1/rdmd.1
  install -Dm644 $srcdir/dmd2/man/man1/dmd.conf.5 $pkgdir/usr/share/man/man5/dmd.conf.5

  install -Dm644 $srcdir/dmd2/license.txt $pkgdir/usr/share/licenses/$pkgname/LICENSE

  mkdir -p $pkgdir/usr/share/d/samples/
  cp -r $srcdir/dmd2/samples/d/* $pkgdir/usr/share/d/samples/

  find $pkgdir/usr -type f | xargs chmod 0644
  chmod 755 $pkgdir/usr/bin/*

  install -Dm755 $srcdir/dmd2/linux/bin${_archbits}/ddemangle $pkgdir/usr/bin/ddemangle
  install -Dm755 $srcdir/dmd2/linux/bin${_archbits}/dman $pkgdir/usr/bin/dman
  install -Dm755 $srcdir/dmd2/linux/bin${_archbits}/dumpobj $pkgdir/usr/bin/dumpobj
  install -Dm755 $srcdir/dmd2/linux/bin${_archbits}/obj2asm $pkgdir/usr/bin/obj2asm
  install -Dm755 $srcdir/dmd2/linux/bin${_archbits}/rdmd $pkgdir/usr/bin/rdmd
  #install -Dm755 $srcdir/D-Programming-Language-tools-*/rdmd $pkgdir/usr/bin/rdmd
}

package_libphobos() {
  pkgdesc="The phobos D standard library for DMD"
  install -Dm644 $srcdir/dmd2/src/druntime/lib/libdruntime-linux${_archbits}.a $pkgdir/usr/lib/libdruntime.a
  install -Dm644 $srcdir/dmd2/src/phobos/generated/linux/release/$_archbits/libphobos2.a $pkgdir/usr/lib/libphobos2.a

  mkdir -p $pkgdir/usr/include/d
  cp -r $srcdir/dmd2/src/phobos/{*.d,etc,std} $pkgdir/usr/include/d

  mkdir -p $pkgdir/usr/include/d/druntime
  cp -r $srcdir/dmd2/src/druntime/import $pkgdir/usr/include/d/druntime

  mkdir -p $pkgdir/usr/share/doc/d/
  cp -r $srcdir/dmd2/html $pkgdir/usr/share/doc/d/

  find $pkgdir/usr -type f | xargs chmod 0644

  install -Dm644 $srcdir/dmd2/src/druntime/LICENSE $pkgdir/usr/share/licenses/$pkgname/LICENSE
}
